/*
 * Copyright 2012 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.expenses.client.activities.ed;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.dialog.Dialogs;
import com.googlecode.mgwt.ui.client.widget.HeaderButton;
import com.googlecode.mgwt.ui.client.widget.HeaderPanel;
import com.googlecode.mgwt.ui.client.widget.MDateBox;
import com.googlecode.mgwt.ui.client.widget.MNumberTextBox;
import com.googlecode.mgwt.ui.client.widget.MTextArea;
import com.googlecode.mgwt.ui.client.widget.MTextBox;
import com.googlecode.mgwt.ui.client.widget.ProgressBar;
import com.googlecode.mgwt.ui.client.widget.ScrollPanel;
import com.googlecode.mgwt.ui.client.widget.base.ButtonBase;

public class ExpenseDetailViewMobileGwtImpl extends Composite implements ExpenseDetailView {

	private static ExpenseDetailViewMobileGwtImplUiBinder uiBinder = GWT.create(ExpenseDetailViewMobileGwtImplUiBinder.class);

	interface ExpenseDetailViewMobileGwtImplUiBinder extends UiBinder<Widget, ExpenseDetailViewMobileGwtImpl> {
	}

	@UiField
	ButtonBase editButton;

	@UiField
	MTextArea descriptionBox;

	@UiField
	MDateBox dateBox;

	@UiField
	MNumberTextBox numberBox;

	@UiField
	HTML title;

	@UiField
	ScrollPanel scrollPanel;

	@UiField
	ProgressBar progressBar;

	@UiField
	ButtonBase cameraButton;

	@UiField
	Image fotoImage;

	@UiField
	FlowPanel dataContainer;

	@UiField
	Panel fotoContainer;

	@UiField
	MTextBox titleBox;

	@UiField
	HeaderPanel headerPanel;

	@UiField
	HeaderButton leftButton;

	private Presenter presenter;

	public ExpenseDetailViewMobileGwtImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		if (MGWT.getOsDetection().isIPhone() || MGWT.getOsDetection().isRetina()) {
			leftButton.setBackButton(true);
			headerPanel.setLeftWidget(leftButton);
		} else {
			if (MGWT.getOsDetection().isTablet()) {
				headerPanel.setLeftWidget(leftButton);
				leftButton.addStyleName(MGWTStyle.getTheme().getMGWTClientBundle().getUtilCss().portraitonly());

			} else {
				leftButton.setVisible(false);
			}
		}

	}

	@Override
	public HasValue<String> getDescription() {
		return descriptionBox;
	}

	@Override
	public HasValue<String> getAmount() {
		return numberBox;
	}

	@Override
	public HasValue<Date> getDate() {
		return dateBox;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;

	}

	@Override
	public HasText getLeftButton() {
		return leftButton;
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		dateBox.setReadOnly(readOnly);
		numberBox.setReadOnly(readOnly);
		descriptionBox.setReadOnly(readOnly);
		titleBox.setReadOnly(readOnly);

	}

	@UiHandler("editButton")
	public void onEditButtonSelected(TapEvent event) {
		if (presenter != null) {
			presenter.onEditButtonPressed();
		}
	}

	@UiHandler("leftButton")
	public void onLeftButtonSelected(TapEvent event) {
		if (presenter != null) {
			presenter.onLeftButtonPressed();
		}
	}

	@UiHandler("cameraButton")
	public void onCameraButtonSelected(TapEvent event) {
		if (presenter != null) {
			presenter.onCameraButtonPressed();
		}
	}

	@Override
	public HasText getEditButton() {
		return editButton;
	}

	@Override
	public HasText getTitleText() {

		return new HasText() {

			@Override
			public void setText(String text) {
				title.setText(text);
				titleBox.setText(text);

			}

			@Override
			public String getText() {
				return titleBox.getText();
			}
		};

	}

	@Override
	public void showError(String string) {
		Dialogs.alert("Error", string, null);

	}

	@Override
	public void setLoading(boolean loading) {

		progressBar.setVisible(loading);
		dataContainer.setVisible(!loading);
		scrollPanel.refresh();

	}

	@Override
	public void showCameraButton(boolean show) {
		cameraButton.setVisible(show);

	}

	@Override
	public void setImageUrl(String data) {
		fotoImage.setUrl(data);

	}

	@Override
	public void showFoto(boolean show) {
		fotoContainer.setVisible(show);
		scrollPanel.refresh();

	}

}
